package xxm.utility.main;

import xxm.utility.event.BasicNetEvent;
import xxm.utility.event.BasicNetEventListener;
import xxm.utility.exception.BasicException;
import xxm.utility.pack.BasicPacket;
import xxm.utility.pack.BasicTcpPacket;
import xxm.utility.socket.BasicAddress;
import xxm.utility.socket.BasicTcpInterface;

public class UpdateFileClient implements BasicNetEventListener , Runnable{
	
	private BasicTcpInterface socketMgr = new BasicTcpInterface();
	
	public void CloseUpdate() {
		socketMgr.CloseSocket(0);
	}
	
	@Override
	public void run() {
		try {
		// TODO Auto-generated method stub
		BasicAddress addr = new BasicAddress();
		addr.setIp("127.0.0.1");
		addr.setNone(true);
		addr.setPort((short)6000);
		
		socketMgr.InitSocket(addr);
		
		BasicTcpPacket pack = new BasicTcpPacket(1024);
		
		UploadPicRequest request = new UploadPicRequest();
		request.header.setCmdId((byte)0xF2);
		//request.header.setCmdVer((byte)127);
		request.header.setCmdVer((byte)0);
		request.projectId = 1;
		request.subTypeId = 4;
		request.preValue = 1;
		request.fileName.setStrData("baa45878901234567890123456789032");
		request.fileExt.setStrData("png");
		
		request.fixValue = (short)(request.preValue ^ (short)0xB16D);
		
		pack.setUsercrc((byte)0);
		//pack.setCrcValue(0);
		
		request.formatBasicPacket(pack);
		socketMgr.addBasicNetEventListener(this);
		BasicTcpPacket recvPack = new BasicTcpPacket(1024);
		socketMgr.sendSynBasicPacket(pack, recvPack);
		for(int i=0;i<100;i++) 
			socketMgr.sendToBasicPacket(pack);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	@Override
	public void actionPerformed(BasicNetEvent e) {
		// TODO Auto-generated method stub
		if( e != null ) {
			UploadPicResponse response = new UploadPicResponse();
			BasicPacket pack = (BasicPacket)e.getPack();
			
			pack.setPosition(0);
			response.parserBasicPacket(pack);
			
			BasicException ec = e.getException();
			
			System.out.println("actionPerformed");
			System.out.println("coder = " + response.errorCode );
			System.out.println("error coder = " + e.getErrorCode() );
			System.out.println(response.fileName.getStrData());
			System.out.println(response.Url.getStrData());

			System.out.println("end=============================");
		}
	}

}
